package controllers;

import models.User;
import controllers.api.API;
import play.mvc.Controller;

public class Settings extends API{
	private static String uid(){
		return session.get("sessionUser");
	}
	 public static void ChengEmail() {
		 String nemail=request.params.get("nemail");
		 String pass=request.params.get("password");
		 validation.required("email",nemail).message("Invalid Email");
		 validation.email("email",nemail).message("Invalid Email");
		 if(validation.hasErrors()){
	        	json(validation.errors());
		 }
		 else{
			 User us=User.find("by_id",uid()).first();
			 if(us.getPassword().equals(pass)){
				 us.setEmail(nemail);
				 us.save();
			 }
			 else json("Error");
		 }
	 }
	 public static void ChengPass(){
		 String pass=request.params.get("pass");
		 String npass=request.params.get("npass");
		 String conf=request.params.get("conf");
		 validation.required("password",npass).message("Invalid Password");
	     validation.minSize("password",npass, 6).message("Invalid Password");
	     if(validation.hasErrors()){
	        	json(validation.errors());
		 }else{
			 User us=User.find("by_id",uid()).first();
			 if(us.getPassword().equals(pass) && npass.equals(conf)){
				 us.setPassword(npass);
				 us.save();
			 }
			 else json("Error");
		 }
	 }
	 public static void DeleteProf(){
		 String pass=request.params.get("pass");
		 User us=User.find("by_id",uid()).first();
		 if(us.getPassword().equals(pass)){
			 us.delete();
		 }
		 else json("Error");
	 }
	 public static void ChengBd(){
		 String pass=request.params.get("pass");
		 String month=request.params.get("month");
		 String day=request.params.get("day");
		 String year=request.params.get("year");
		 User us=User.find("by_id",uid()).first();
		 if(us.getPassword().equals(pass)){
			 if(!month.equals("0")){
				 us.setMonth(month);
				 us.save();
			 }
			 if(!day.equals("Day")){
				 us.setDay(day);
				 us.save();
			 }
			 if(!year.equals("Year")){
				 us.setYear(year);
				 us.save();
			 }
		 }
	 }
}
